package com.cl.park.action;

import lombok.Data;
import lombok.EqualsAndHashCode;

import com.cl.park.model.AjaxData;
import com.cl.park.model.AjaxResponse;
import com.cl.park.model.Entrance;
import com.cl.park.model.Floor;
import com.cl.park.model.Park;
import com.cl.park.service.AdminService;
import com.opensymphony.xwork2.ActionSupport;

@Data
@EqualsAndHashCode(callSuper=false)
public class ParkManageAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	private AjaxData<Object> data;
	
	private AjaxResponse result;
	
	private Entrance entrance;
	
	private Floor floor;
	
	private Park park;
	
	private Integer id;
	
	private AdminService adminService;
	
	public String listEntrance(){
		return SUCCESS;
	}
	
	public String getEntranceList(){
		data = new AjaxData<Object>(0, 0, adminService.entranceList());
		return SUCCESS;
	}
	
	public String editEntrance(){
		if(null != id){
			entrance = adminService.getEntranceById(id);
		}else{
			entrance = null;
		}
		id = null;
		return SUCCESS;
	}
	
	public String saveEntrance(){
		if(null != entrance && null != entrance.getId()){
			adminService.updateEntrance(entrance);
			result = new AjaxResponse(true, "修改成功");
		}else{
			adminService.addEntrance(entrance);
			result = new AjaxResponse(true, "添加成功");
		}
		return SUCCESS;
	}
	
	public String listFloor(){
		data = new AjaxData<Object>(0, 0, adminService.floorList());
		return SUCCESS;
	}
	
	public String getFloorList(){
		data = new AjaxData<Object>(0, 0, adminService.floorList());
		return SUCCESS;
	}
	
	public String editFloor(){
		if(null != id){
			floor = adminService.getFloorById(id);
		}else{
			floor = null;
		}
		id = null;
		return SUCCESS;
	}
	
	public String saveFloor(){
		if(null != floor && null != floor.getId()){
			adminService.updateFloor(floor);
			result = new AjaxResponse(true, "修改成功");
		}else{
			adminService.addFloor(floor);
			result = new AjaxResponse(true, "添加成功");
		}
		return SUCCESS;
	}
	
	public String listPark(){
		data = new AjaxData<Object>(0, 0, adminService.parkList(park));
		return SUCCESS;
	}
	
	public String getParkList(){
		data = new AjaxData<Object>(0, 0, adminService.parkList(park));
		return SUCCESS;
	}
	
	public String editPark(){
		if(null != park && null != park.getId() && 0 == park.getState()){
			park = adminService.getPark(park);
		}else{
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String savePark(){
		if(null != park && null != park.getId()){
			adminService.updatePark(park);
			result = new AjaxResponse(true, "修改成功");
		}else{
			adminService.addPrak(park);
			result = new AjaxResponse(true, "添加成功");
		}
		return SUCCESS;
	}

}
